<template>
  <view class="page_box">
    <view class="box">
      <view class="title">提现金额</view>
      <up-input v-model="formData.amount" type="number" class="input_num" fontSize="30rpx" placeholder="请输入数量" border="bottom" clearable>
        <template #prefix> <text style="font-size: 40rpx">￥</text> </template>
        <template #suffix>
          <text class="all_text" @click="allClick">全部提现</text>
        </template>
      </up-input>
      <view class="des">提现金额为100以上且是100的整数倍，到账时间：T+2</view>
      <view class="des">
        当前余额：
        <text>￥{{ banlanceAmont }}</text>
      </view>
    </view>

    <view class="box">
      <view class="title">
        <text>提现方式</text>
        <view class="btn" @click="goPage('/pages/myPage/setting/editPage?pageType=3')">去绑定</view>
      </view>
      <view class="zfbbox">
        <view class="left">
          <u-icon name="zhifubao-circle-fill" color="#1676fd" size="30"></u-icon>
          <view class="textBox">
            <view>提现到支付宝</view>
            <view>实时到账</view>
          </view>
        </view>
        <view class="">
          <u-radio-group v-model="radiovalue1" activeColor="#1676fd">
            <u-radio name="1"></u-radio>
          </u-radio-group>
        </view>
      </view>
      <view class="sxf">手续费：{{ withdrawal_fee * 100 }}%</view>
    </view>
    <u-modal :show="showModal" title="请输入支付密码" showCancelButton @confirm="pay" @cancel="closeFn">
      <view class="popbox">
        <u-input v-model="formData.payPassword" placeholder="请输入支付密码" :password="isPass">
          <template #suffix>
            <u-icon name="eye-off" color="#bfbfbf" size="28" v-if="isPass" @click="isPass = false"></u-icon>
            <u-icon name="eye" color="#bfbfbf" size="28" v-else @click="isPass = true"></u-icon>
          </template>
        </u-input>
      </view>
    </u-modal>
    <view class="buttomBox">
      <u-button type="primary" color="#1676fd" shape="circle" @click="submit">立即提现</u-button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { configKey, withdrawal } from "@/api/my";
import { goPage } from "@/utils/commonFn";
import { onBeforeMount, onMounted, ref } from "vue";
const { banlanceAmont } = defineProps<{
  banlanceAmont: string;
}>();
const showModal = ref(false);
const payPassword = ref(null);
const formData = ref({
  amount: 0,
  payPassword: "",
  userId: uni.getStorageSync("userId"),
});
const isPass = ref(true);
const radiovalue1 = ref("1");
const withdrawal_fee = ref(0);
onMounted(async () => {
  const res = await configKey("withdrawal_fee");
  withdrawal_fee.value = res.data;
});
const allClick = () => {
  if (+banlanceAmont === 0) {
    uni.showToast({
      title: "暂无余额",
      icon: "none",
    });
  }
  formData.value.amount = +banlanceAmont;
};
const submit = () => {
  if (!formData.value.amount) {
    uni.showToast({
      title: "余额不能为0",
      icon: "none",
    });
    return;
  }
  showModal.value = true;
};
const closeFn = () => {
  showModal.value = false;
  payPassword.value = null;
};
const pay = async () => {
  try {
    await withdrawal(formData.value);
    uni.showToast({
      title: "提交成功",
      complete: () => {
        setTimeout(() => {
          uni.navigateBack();
        }, 500);
      },
    });
  } catch (error: any) {
    if (error.code === 552) {
      uni.showModal({
        title: "提示",
        content: "未设置支付密码，是否前往",
        success: function (res) {
          if (res.confirm) {
            // 执行确认后的操作
            uni.navigateTo({
              url: "/pages/myPage/setting/editPage?pageType=2",
            });
          }
        },
      });
      return;
    }
    if (error.code === 554) {
      uni.showModal({
        title: "提示",
        content: "未绑定支付宝信息，是否前往",
        success: function (res) {
          if (res.confirm) {
            // 执行确认后的操作
            uni.navigateTo({
              url: "/pages/myPage/setting/editPage?pageType=3",
            });
          }
        },
      });
      return;
    }
  }
};
</script>

<style lang="less" scoped>
.box {
  background: #ffffff;
  margin-bottom: 20rpx;
  padding: 32rpx 40rpx;
  .title {
    margin-bottom: 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    text {
      font-size: 32rpx;
      font-weight: 600;
      color: #333333;
      line-height: 44rpx;
    }
    .btn {
      border-radius: 40rpx;
      border: 2rpx solid #f7f7f7;
      font-size: 28rpx;
      color: #333333;
      line-height: 40rpx;
      padding: 8rpx 20rpx;
    }
  }
  .input_num {
    height: 150rpx;
  }
  .des {
    font-size: 28rpx;
    font-weight: 400;
    color: #bebebe;
    margin-top: 38rpx;
    text {
      color: #1676fd;
    }
  }
  /deep/ .u-cell__body {
    padding-left: 0px;
    padding-right: 0px;
  }
}
.img {
  width: 64rpx;
  height: 64rpx;
  margin-right: 32rpx;
}
.zfbbox {
  display: flex;
  justify-content: space-between;
  margin: 30rpx 0;
  align-items: center;
  padding-bottom: 30rpx;
  border-bottom: 1px solid #f7f7f7;

  .left {
    display: flex;
    .img {
      width: 56rpx;
      height: 56rpx;
    }
    .iconBox {
      position: relative;
      margin-right: 14rpx;
      .img,
      .icon {
        width: 38rpx;
        height: 38rpx;
        position: absolute;
        left: 20rpx !important;
        top: 20rpx !important;
      }
      .icon {
        left: 0;
        top: 0;
      }
    }
    .textBox {
      font-size: 30rpx;
      color: #333333;
      margin-left: 30rpx;
      & > view {
        &:last-child {
          color: #a6a9af;
          font-size: 24rpx;
        }
      }
    }
  }
}
.all_text {
  font-size: 28rpx;
  font-weight: 400;
  color: #1676fd !important;
}
/deep/ .u-border-bottom {
  border-color: #f7f7f7 !important;
}
.sxf {
  font-size: 26rpx;
  font-family: DINPro, DINPro;
  font-weight: 400;
  color: #1e1e1e;
  line-height: 34rpx;
}
</style>
